package br.com.puertorico.model.querybuilder.sorting;

import java.io.Serializable;

/**
 *
 * @author Daniel Camargo
 *
 */
public class SortItem implements Comparable<SortItem>, Serializable {

	private static final long serialVersionUID = 3384981388191007141L;

	/**
	 * A posicao que este campo ocupara na lista de itens de ordenacao
	 */
	private Integer order;
	/**
	 * Define se a ordenacao sera ascendente ou nao (descendente)
	 */
	private Boolean asc;
	/**
	 * O nome da coluna no padrao hql.
	 */
	private String columnName;

	public SortItem(String columnName, Boolean asc, Integer order) {
		if (order == null) {
			throw new IllegalArgumentException();
		}
		this.order = order;
		this.asc = asc;
		this.columnName = columnName;
	}

	public Integer getOrder() {
		return order;
	}

	public void setOrder(Integer order) {
		this.order = order;
	}

	public Boolean getAsc() {
		return asc;
	}

	public void setAsc(Boolean asc) {
		this.asc = asc;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	/**
	 * @return ordenacao
	 */
	public int compareTo(SortItem o) {
		if (o != null) {
			return order.compareTo(o.getOrder());
		}
		return 0;
	}

	@Override
	public int hashCode() {
		return order.hashCode() + columnName.hashCode();
	}

	@Override
	public String toString() {
		return columnName + "@" + (asc ? "ASC" : "DESC");
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof SortItem)) {
			return false;
		}
		return ((SortItem) obj).getColumnName().equals(columnName);
	}

}
